<?php

namespace app\api\model\examination;

use app\common\exception\BaseException;
use app\common\model\examination\ExaminationLog as LogModel;


/**
 * 简历模型
 */
class Log extends LogModel
{
    /**
     * 隐藏字段
     */
    protected $hidden = [];

    //获取列表
    public function getList($data)
    {
        $model = $this;
        $sort = ['duration' => 'asc', 'score' => 'desc'];
        $list = $model->with(['user'])
            ->where('status', '=', 0)
            ->order($sort)
            ->field("*")
            ->paginate($data);
        return $list;
    }

    //我的排名
    public function getMyList($data,$user)
    {

        $model = $this;
        $sort = ['duration' => 'asc', 'score' => 'desc'];
        $list = $model->with(['user'])
            ->where('status', '=', 0)
            ->order($sort)
            ->field("id,user_id,status")
            ->select()->toArray();

        $i=0;
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $i++;
                if($v['user_id']==$user->user_id){
                    break;
                }
            }
        }
        return $i;
    }

    //简历详情
    public function getDetail($hire_id, $user = false)
    {
        $detail = self::detail($hire_id);
        if (!$detail) {
            throw new BaseException(['msg' => "招聘新信息不存在"]);
        }
        $this->addBrowse($hire_id);
        return $detail;
    }

    //添加浏览数
    public function addBrowse($hire_id)
    {
        $this->where('hire_id', '=', $hire_id)->inc('browse_count', 1)->update();
    }

    //获取列表
    public function getLlistarr()
    {
        $model = $this;
        $sort = ['duration' => 'asc', 'score' => 'desc'];
        $list = $model->with(['user'])
            ->where('status', '=', 0)
            ->order($sort)
            ->field("*")
            ->select()->toArray();
        return $list;
    }


    /**
     * 立即购买：获取订单简历列表
     */
    public static function getOrderCourseListByNow($params)
    {
        // 商品详情
        $course = self::detail($params['hire_id']);
        // 商品列表
        $courseList = [$course];
        foreach ($courseList as &$item) {
            // 商品购买数量
            $item['total_num'] = 1;
            // 商品购买数量
            $item['product_price'] = $item['course_price'];
            // 商品购买总金额
            $item['total_price'] = helper::bcmul($item['course_price'], $item['total_num']);
            //商品id
            $item['product_id'] = $item['hire_id'];
        }
        return $courseList;
    }

    //获取列表
    public function getListByIdsFromApi($courseIds, $course_type)
    {
        $list = $this->with(['image'])
            ->where('course_type', '=', $course_type)
            ->where('is_show', '=', 0)
            ->where('is_delete', '=', 0)
            ->where('hire_id', 'in', $courseIds)
            ->order(['sort' => 'asc', 'hire_id' => 'desc'])
            ->select();
        return $list;
    }
}